from django.views.decorators.csrf import csrf_exempt
from base.myjson import JSONResponse
from app.model.MemberModel import QyMember,MemberAll
from base.mybase64 import MyFile
from base.wechat_sdk.basic import WechatBasic
from django.db import transaction
from base.Var import appid,appsecret
import json
import time


@csrf_exempt
def MemberRegistration(request):
    '''
    使用户一进入游戏就获取该用户'openid'并在后台注册用户信息;
    如果用户未注册,则注册用户信息;
    如果用户已注册,则更新用户头像及昵称(如果用户未在游戏内更改头像的话)
    :param request:
    :return:
    '''
    if request.method == 'GET':
        try:
            data = request.GET
            users = QyMember.objects.filter(openid=data.get('openid'))
            # 判断用户是否已注册
            if users:
                if str(users[0].avatar).startswith('http'):
                    user = QyMember.objects.get(openid=data.get('openid'))
                    user.avatar = data.get('media_id')
                    user.save()
                else:
                    pass
                # 获取登录用户信息
                user_info = QyMember.objects.filter(openid=data.get('openid'))
                # 返回JSON字符串
                serializer = MemberAll(user_info, many=True)
                return JSONResponse(0,serializer.data)
            else:
                user = QyMember(
                    avatar=data.get('media_id'),
                    openid=data.get('openid'),
                    unionid=data.get('unionid'),
                    nickname=data.get('nickname'),
                    add_time=str(time.time())
                )
                user.save()
                #获取登录用户信息
                user_info = QyMember.objects.filter(openid=data.get('openid'))
                #返回JSON字符串
                serializer = MemberAll(user_info, many=True)
                return JSONResponse(0,serializer.data)
        except:
            return JSONResponse(1000)
    else:
        return JSONResponse(1054)


@csrf_exempt
def get_member_info(request):
    '''
    获取当前用户信息
    '''
    if request.method == 'GET':
        try:
            user_info = QyMember.objects.filter(openid=request.GET.get('openid'))
            # 返回JSON字符串
            serializer = MemberAll(user_info, many=True)
            return JSONResponse(0,serializer.data)
        except:
            return JSONResponse(1000)
    else:
        return JSONResponse(1054)


@csrf_exempt
def change_member_info(request):
    '''
    根据请求信息对当前用户信息进行更改
    :param request:
    :return:
    '''
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            open_id = data.get('openid')
            # 获取登录用户
            user = QyMember.objects.get(openid=open_id)
            with transaction.atomic():
                # 判断是否上传图片ID
                if data.get('media_id'):
                    obj = WechatBasic(appid=appid, appsecret=appsecret)
                    response = obj.download_media(data.get('media_id'))
                    file, st = MyFile().Picture_download(response)
                    user.avatar = st
                user.unionid = data.get('unionid')
                user.age = data.get('age')
                user.nickname = data.get('nickname')
                user.profession = data.get('profession')
                user.phonenum = data.get('phonenum')
                user.balance = data.get('balance')
                user.save()
            user_info = QyMember.objects.filter(openid=open_id)
            # 返回JSON字符串
            serializer = MemberAll(user_info, many=True)
            return JSONResponse(0,serializer.data)
        except:
            return JSONResponse(1000)
    else:
        return JSONResponse(1054)